#include "display_task.h"

SettingMode_t settingMode = {
    INDEX_SET_N1,       // 起始下标
    0,                  //当前码值
    NULL,               // 目标码值地址
    true,               // 闪烁
    false,              // 设置中
    NULL,               // 源数组
    0,                  // 源数组长度
};
static int8_t s_ModeArry[8] = {0};

void Btn_Confirm(void);
void Btn_Select(void);
void Btn_Up(void);
void Btn_Down(void);
void Btn_Sys1_Open(void);
void Btn_Sys2_Open(void);
void ItemBlink(void);
void DisplayTaskInit(void);
void DisplayTask(void);

void Btn_Init(void)
{
    BtnRegister(BTN2, Btn_Select, BTN_CB_FUNC);
    BtnRegister(BTN3, Btn_Up, BTN_CB_FUNC);
    BtnRegister(BTN3, Btn_Up, BTN_CB_LONG);
    BtnRegister(BTN4, Btn_Down, BTN_CB_FUNC);
    BtnRegister(BTN4, Btn_Down, BTN_CB_LONG);
}
void Btn_DeInit(void)
{
    BtnRegister(BTN2, NULL, BTN_CB_FUNC);
    BtnRegister(BTN3, NULL, BTN_CB_FUNC);
    BtnRegister(BTN3, NULL, BTN_CB_LONG);
    BtnRegister(BTN4, NULL, BTN_CB_FUNC);
    BtnRegister(BTN4, NULL, BTN_CB_LONG);
}

void Analy_ModbusData(void)
{
    int i = 0;
 // 模拟加载设置数据     66.6  66.6
     for(i = 0; i < 6; i++)
        settingMode.SourceArry[i] = 6;
    *posCtrl.num1 = SEG_CODE[6];
    *posCtrl.num2 = SEG_CODE[6];
    *posCtrl.num3 = SEG_CODE[6];
    *posCtrl.num4 = SEG_CODE[6];
    *posCtrl.unit1 = SEG_CODE[6];
    *posCtrl.unit2 = SEG_CODE[6];
    *posCtrl.display1 |= POS_POINT;
    *posCtrl.display2 |= POS_POINT;
    CLR_REG_BIT(*posCtrl.display1, POS_MINUS);  
    CLR_REG_BIT(*posCtrl.display2, POS_MINUS);  
    settingMode.SourceArry[INDEX_SET_M1] = 0;
    settingMode.SourceArry[INDEX_SET_M2] = 0;

}

void Btn_Confirm(void)
{
    int sy1 = 0;
    int sy2 = 0;
    settingMode.CurrIndex = 0;

    if(settingMode.IsSetting)      // 退出设置温度模式
    {   
        Btn_DeInit();
        sy1 = settingMode.SourceArry[INDEX_SET_N1] * 100 + settingMode.SourceArry[INDEX_SET_N2] * 10 + 
              settingMode.SourceArry[INDEX_SET_F1];
        sy2 = settingMode.SourceArry[INDEX_SET_N3] * 100 + settingMode.SourceArry[INDEX_SET_N4] * 10 + 
              settingMode.SourceArry[INDEX_SET_F2];
        if(settingMode.SourceArry[INDEX_SET_M1] != 0)       
            sy1 = 0 - sy1;
        
        if(settingMode.SourceArry[INDEX_SET_M2] != 0)        
            sy2 = 0 - sy2;
  
DEBUG_PRINT("sy1: %d sy2: %d\n", sy1, sy2);
        GN1640_ClearRAM();
    }
    else        // 进入设置温度模式
    {
        settingMode.Blink = true;
        Btn_Init();
        Analy_ModbusData();
        settingMode.TarItemCode = WritableList[settingMode.CurrIndex];
        settingMode.CurrItemCode = *settingMode.TarItemCode;
    }
    GN1640_Refresh();
    settingMode.IsSetting = !settingMode.IsSetting;             // 模式取反
}

void Btn_Select(void)
{
    settingMode.Blink = TRUE;
    // 如果上一个是负号 
    if(settingMode.CurrIndex == INDEX_SET_M1)
    {
        if(settingMode.SourceArry[INDEX_SET_M1])
            SET_REG_BIT(settingMode.CurrItemCode, POS_MINUS);
        else CLR_REG_BIT(settingMode.CurrItemCode, POS_MINUS); 
    }else if(settingMode.CurrIndex == INDEX_SET_M2)
    {
        if(settingMode.SourceArry[INDEX_SET_M2])
            SET_REG_BIT(settingMode.CurrItemCode, POS_MINUS);
        else CLR_REG_BIT(settingMode.CurrItemCode, POS_MINUS);  
    }
    *settingMode.TarItemCode = settingMode.CurrItemCode;        // 设置上一个数据
    GN1640_Refresh();
    settingMode.CurrIndex++;  
    if(settingMode.CurrIndex >= settingMode.SourceLen)
        settingMode.CurrIndex = 0;

    settingMode.TarItemCode = WritableList[settingMode.CurrIndex]; //获取当前的数据
    // 用于闪烁
    settingMode.CurrItemCode = *settingMode.TarItemCode;        
    if(settingMode.CurrIndex == INDEX_SET_M1)
    {
        settingMode.CurrItemCode |= POS_MINUS;
    }else if(settingMode.CurrIndex == INDEX_SET_M2)
    {
        settingMode.CurrItemCode |= POS_MINUS;
    }
    GN1640_Refresh();
}

void Btn_Up(void)
{
    int8_t num = 0;
    settingMode.Blink = FALSE;
    if(settingMode.CurrIndex == INDEX_SET_M1 || settingMode.CurrIndex == INDEX_SET_M2)
    {
        *settingMode.TarItemCode |= (POS_MINUS | POS_POINT);
        settingMode.SourceArry[settingMode.CurrIndex] = 1;
        goto end;
    }
    num = FindNumIndexFromSEG(settingMode.CurrItemCode);
    if(num == ERR_ITEM)
        settingMode.CurrItemCode = SEG_CODE[0];
    num++;
    if(num > 9)     num = 0;
    settingMode.CurrItemCode = SEG_CODE[num];
    settingMode.SourceArry[settingMode.CurrIndex] = num;
    settingMode.Blink = TRUE;
 end:     
    GN1640_Refresh();
}

void Btn_Down(void)
{
    int8_t num = 0;
    settingMode.Blink = FALSE;
    if(settingMode.CurrIndex == INDEX_SET_M1 || settingMode.CurrIndex == INDEX_SET_M2)
    {
        *settingMode.TarItemCode &= (~POS_MINUS);
        settingMode.SourceArry[settingMode.CurrIndex] = 0;
        goto end;
    }    
    num = FindNumIndexFromSEG(settingMode.CurrItemCode);
    if(num == ERR_ITEM)
        settingMode.CurrItemCode = SEG_CODE[0];
    num--;
    if(num < 0)     num = 9;
    settingMode.CurrItemCode = SEG_CODE[num];
    settingMode.SourceArry[settingMode.CurrIndex] = num;
    settingMode.Blink = TRUE;
end:
    GN1640_Refresh();
}

void Btn_Sys1_Open(void)
{
    // 开关机1
}

void Btn_Sys2_Open(void)
{
    // 开关机2
}

void ItemBlink(void)
{
    // static uint32_t lastTick = 0;
    static BOOL toggle = FALSE;
    SegCode_t seg = 0;

    if(!settingMode.Blink)          return;
    if(!settingMode.IsSetting)      return;
    // if(GetTick() - lastTick < 300)  return;
    // lastTick = GetTick();
    
    if(settingMode.CurrIndex == INDEX_SET_M1 || settingMode.CurrIndex == INDEX_SET_M2)   
        seg = toggle ? SET_REG_BIT(settingMode.CurrItemCode, POS_MINUS) : CLR_REG_BIT(settingMode.CurrItemCode, POS_MINUS);      // 只闪烁负号
    else    
        seg = toggle ? settingMode.CurrItemCode : 0;
    *settingMode.TarItemCode = seg;    
    toggle = !toggle;
    GN1640_Refresh();
}

void DisplayTaskInit(void)
{
    settingMode.SourceArry = s_ModeArry;
    settingMode.SourceLen = sizeof(s_ModeArry);
    BtnRegister(BTN1, Btn_Confirm, BTN_CB_FUNC);
}

void DisplayTask(void)
{
    if(!settingMode.IsSetting)          // 非设置模式
    {
        // GN1640_ClearRAM();
        // *posCtrl.num1 = SEG_CODE[8];
        // *posCtrl.num2 = SEG_CODE[8];
        // *posCtrl.num3 = SEG_CODE[8];
        // *posCtrl.num4 = SEG_CODE[8];
        // *posCtrl.unit1 = SEG_CODE[8];
        // *posCtrl.unit2 = SEG_CODE[8];
        // *posCtrl.top   = SEG_CODE[8];
        // *posCtrl.display1 = POS_POINT | POS_MINUS | POS_INFOR | POS_SYSTEM; 
        // *posCtrl.display2 = POS_POINT | POS_MINUS | POS_INFOR | POS_SYSTEM;
        
        // GN1640_Refresh();
    }else ItemBlink();

}
